package com.exam.sacrifice.ui;

import static android.view.View.GONE;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.exam.commonbiz.base.BaseActivity;
import com.exam.commonbiz.base.BaseViewModel;
import com.exam.commonbiz.base.BasicApplication;
import com.exam.commonbiz.util.OnFastClickListener;
import com.exam.commonbiz.util.ToastUtil;
import com.exam.sacrifice.R;
import com.exam.sacrifice.databinding.ActivitySplashBinding;
import com.exam.sacrifice.ui.login.LoginActivity;

public class SplashActivity extends BaseActivity<BaseViewModel, ActivitySplashBinding> {

    private boolean isSkipClicked = false;
    private CountDownTimer countDownTimer;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public void initData() {

//        actionStart(MainActivity.class);
//        finish();

        binding.tvJump.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                isSkipClicked = true;
                binding.videoView.stopPlayback();
                jump();
            }
        });

        initVideo();
    }

    private void initVideo() {

        // 设置视频源 (res/raw/sample.mp4)
        Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.splash_bg);
        binding.videoView.setVideoURI(videoUri);

        // 设置监听器
        binding.videoView.setOnPreparedListener(mp -> {
            binding.videoView.start();
            binding.ivBg.setVisibility(GONE);
            startTimer();
            int dura = binding.videoView.getDuration();
            Log.d("zddddz", "dura:" + dura);
        });

        binding.videoView.setOnCompletionListener(mp -> {
            jump();
        });

        binding.videoView.setOnErrorListener((mp, what, extra) -> {
            ToastUtil.getInstance().showToast("播放错误");
            return true;
        });
    }

    private void startTimer() {
        countDownTimer = new CountDownTimer(binding.videoView.getDuration(), 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                updateCountdownText();
            }

            @Override
            public void onFinish() {

            }
        }.start();
    }

    private void updateCountdownText() {
        int seconds = (binding.videoView.getDuration() - binding.videoView.getCurrentPosition()) / 1000;
        binding.tvJump.setText("跳过(" + seconds + "s)");
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    private void jump() {
        if (TextUtils.isEmpty(BasicApplication.app.getToken())) {
            actionStart(LoginActivity.class);
        } else {
            actionStart(MainActivity.class);
        }
        finish();
    }

    @Override
    public void back() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding.videoView.stopPlayback();
    }
}